#include<string>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;

class Solution {
public:
    int findSubstringInWraproundString(string p) {
        if (p.length() < 2) return p.length();
        vector<int>dp(26, 0);
        int k = 0;
        for (int i = 0; i < p.length(); ++i) {
            if (i > 0 && ((p[i] - p[i - 1] == 1) || (p[i] == 'a' && p[i - 1] == 'z'))) 
                ++k;
            else
                k = 1;
            dp[p[i] - 'a'] = max(dp[p[i] - 'a'], k);

        }
        int res = 0;
        for (int i = 0; i < dp.size(); ++i) {
            res += dp[i];
        }
        return res;
    }
};